const Mongoose = require("mongoose")

const meta = {
    createAt : {
        type : Date , 
        default : Date.now
    },
    updateAt : {
        type : Date , 
        default : Date.now
    }
}

module.exports = class Schema extends Mongoose.Schema{

    constructor( name , opt , tableName ){
        tableName = tableName || name 
        // opt.meta = opt.meta || {}
        opt = Object.assign( opt ,meta )
        super( opt )
        this.name( name )
        this.tableName( tableName )
        this.befre() 
    } 

    name( name ){
        if( name ) this._Name = name 
        return this._Name 
    }

    tableName( name ){
        if( name ) this._TableName = name 
        return this._TableName
    }

    befre(){
        //不再支持update方法

        //自动补充 updateAt 中间件
        this.pre("updateOne" , async function(){
            // this._update.meta = this._update.meta || {}
            this._update.updateAt = Date.now()
        })

        //自动补充 updateAt 中间件
        this.pre("updateMany" , async function(){
            // this._update.meta = this._update.meta || {}
            this._update.updateAt = Date.now()
        }) 

    }

}